package com.reef.dao;


import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.reef.entity.Reader;


public interface ReaderRepository extends JpaRepository<Reader, Integer>,JpaSpecificationExecutor<Reader>{
	/**
	 * 后台根据id和密码登陆
	 *
	 * @return
	 */
	@Query(value = "SELECT * FROM reader where r_id=?1 and password=?2 and staus=?3", nativeQuery = true)  
	Reader findByR_idAndpassword(int r_id,String password,int staus);
	/**
	 * 修改密码
	 */
	@Transactional
	@Modifying
	@Query(value = "update reader set password=?1 where r_id=?2",nativeQuery = true)
	int updatePwd(String pwd,int id);

	/**
	 * 根据id查学生信息
	 */
	@Query(value = "select * from reader where r_id  = ?1",nativeQuery = true)
	Reader findByR_id(int r_id);
	
	/**
	 * 修改借数量，最大借书
	 */
	@Transactional
	@Modifying
	@Query(value = "update reader set borrowbook=borrowbook+?1,bigbook=bigbook-?2 where r_id=?3",nativeQuery = true)
	int updateByBigBook(int borrowbook,int bigbook,int id);
	/**
	 * 修改借数量，最大借书
	 */
	@Transactional
	@Modifying
	@Query(value = "UPDATE reader SET givenbook=givenbook+?1,bigbook=bigbook+?2 WHERE r_id=?3",nativeQuery = true)
	int updateByGiveBook(int givenbook,int bigbook,int id);
	
}
